package com.yuju.interceptors;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.yuju.util.Utils;

/**
 * 跨域拦截器
 * @author xue
 * @date 2016-08-12
 *
 */
public class CorsInterceptor implements HandlerInterceptor {
	String regEx = "http://.*:10001"; //"http://.*.xzjcloud.com";
	//String regEx = "http://.*:10000"; //"http://.*.xzjcloud.com";
	private Pattern xzjcloudPattern = Pattern.compile(regEx);
	
	private String allowOrigin;
	private String allowMethods;
	private String allowCredentials;
	private String allowHeaders;
	private String exposeHeaders;

	public String getAllowOrigin() {
		return allowOrigin;
	}

	public void setAllowOrigin(String allowOrigin) {
		this.allowOrigin = allowOrigin;
	}

	public String getAllowMethods() {
		return allowMethods;
	}

	public void setAllowMethods(String allowMethods) {
		this.allowMethods = allowMethods;
	}

	public String getAllowCredentials() {
		return allowCredentials;
	}

	public void setAllowCredentials(String allowCredentials) {
		this.allowCredentials = allowCredentials;
	}

	public String getAllowHeaders() {
		return allowHeaders;
	}

	public void setAllowHeaders(String allowHeaders) {
		this.allowHeaders = allowHeaders;
	}

	public String getExposeHeaders() {
		return exposeHeaders;
	}

	public void setExposeHeaders(String exposeHeaders) {
		this.exposeHeaders = exposeHeaders;
	}

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg) throws Exception {
		if (null != allowOrigin) {
			List<String> allowOriginList = Arrays.asList(allowOrigin.split(","));
			if (null != allowOriginList && !allowOriginList.isEmpty()) {
				String currentOrigin = request.getHeader("Origin");
				if (allowOriginList.contains(currentOrigin)) {
					response.setHeader("Access-Control-Allow-Origin", currentOrigin);
				}
			}
		}
		String currentOrigin = request.getHeader("Origin");
		System.out.println("===========currentOrigin======" + currentOrigin);
		response.setHeader("Access-Control-Allow-Origin", "*");
		
		if (StringUtils.isNotEmpty(allowMethods)) {
			response.setHeader("Access-Control-Allow-Methods", allowMethods);
		}
		
		if (StringUtils.isNotEmpty(allowCredentials)) {
			response.setHeader("Access-Control-Allow-Credentials", allowCredentials);
		}
		
		if (StringUtils.isNotEmpty(allowHeaders)) {
			response.setHeader("Access-Control-Allow-Headers", allowHeaders);
		}
		
		if (StringUtils.isNotEmpty(exposeHeaders)) {
			response.setHeader("Access-Control-Expose-Headers", exposeHeaders);
		}
		
		/*if (null != allowOrigin) {
			List<String> allowOriginList = Arrays.asList(allowOrigin.split(","));
			if (null != allowOriginList && allowOriginList.size() > 0) {
				String currentOrigin = request.getHeader("Origin");
				for (String string : allowOriginList) {
					if (string.contains("*") == false && string.equals(currentOrigin)) {
						response.setHeader("Access-Control-Allow-Origin", currentOrigin);
						break;
					} else {
						if (string.contains("*")) {
							// 兼容手机端，避免空指针
							if(Utils.isNotNull(currentOrigin)) {
								Matcher xzjcloudMatcher = xzjcloudPattern.matcher(currentOrigin);
								boolean rs = xzjcloudMatcher.matches();
								if(rs) {
									response.setHeader("Access-Control-Allow-Origin", currentOrigin);
									break;
								}
							} 
						}
					}
				}
			}
		}
		
		if (StringUtils.isNotEmpty(allowMethods)) {
			response.setHeader("Access-Control-Allow-Methods", allowMethods);
		}
		
		if (StringUtils.isNotEmpty(allowCredentials)) {
			response.setHeader("Access-Control-Allow-Credentials", allowCredentials);
		}
		
		if (StringUtils.isNotEmpty(allowHeaders)) {
			response.setHeader("Access-Control-Allow-Headers", allowHeaders);
		}
		
		if (StringUtils.isNotEmpty(exposeHeaders)) {
			response.setHeader("Access-Control-Expose-Headers", exposeHeaders);
		}*/
		
		return true;
	}

}
